package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.admin.dao.ShopImageDao;
import com.shop.admin.entity.ShopImage;
import com.shop.admin.service.ShopImageService;
import com.shop.admin.vo.Images;
import com.shop.admin.vo.ImagesIdVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

//TODO:这一块和其他功能都有一样的问题 就是过度使用了不安全的写法 很多地方你没有强校验 查的接口可以不需要 但是涉及到修改的 你必须强校验
@Service
public class ShopImageServiceImpl extends ServiceImpl<ShopImageDao, ShopImage> implements ShopImageService {
    @Override
    public void saveImages(Long orderId, List<Images> imageVOList) {
        if(imageVOList == null || imageVOList.size() == 0){

        }else{
            List<ShopImage> collect = imageVOList.stream().map(img -> {
                ShopImage shopImage=new ShopImage();
                shopImage.setOrderId(orderId);
                shopImage.setImgUrl(img.getImgUrl());
                shopImage.setDefaultImg(img.getDefaultImg());

                return shopImage;
            }).collect(Collectors.toList());

            this.saveBatch(collect);
        }
    }

    @Override
    public List<ShopImage> selectOrderImages(Integer imageId) {
        List<ShopImage> shopImages = this.list(new QueryWrapper<ShopImage>().eq("order_id", imageId));
        return shopImages;
    }

    @Override
    @Transactional
    public void updateImages(Long orderId, List<Images> imagesList) {
        this.baseMapper.delete(new LambdaQueryWrapper<ShopImage>().eq(ShopImage::getOrderId,orderId));

        for (int i = 0; i < imagesList.size(); i++) {
            ShopImage shopImage=new ShopImage();
            shopImage.setOrderId(orderId);
            shopImage.setImgUrl(imagesList.get(i).getImgUrl());
            shopImage.setDefaultImg(imagesList.get(i).getDefaultImg());
            this.baseMapper.insert(shopImage);
        }
    }

    @Override
    public void removeImagesByOrderId(List<Long> asList) {
        for (Long aLong : asList) {
            this.baseMapper.delete(new QueryWrapper<ShopImage>().lambda().eq(ShopImage::getOrderId,aLong));
        }

    }

    @Override
    public List<ImagesIdVo> selectImageList(List<Long> orderId) {
        return this.baseMapper.selectImageList(orderId);
    }
}
